package com.health.platform.common.utils;

import com.health.platform.common.core.entity.EchartsEntity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangyanlei
 */
public class MyUtils {

    public static String chuFa(int a, int b) {
        DecimalFormat dF = new DecimalFormat("0.00");
        if(a==0){
            return "0";
        }else if(b==0){
            return "1";
        }
        return dF.format((float) a / b);
    }

    /**
     * 计算同比同比=（页面选择的日期-去年同期）/去年同期 ）
     */
    public static String getProportion(Integer thisYearGrade, Integer lastYearGrade) {
        if (lastYearGrade == null) {
            return "缺少所选日期上一年同期数据";
        }
        if (thisYearGrade == null) {
            return "缺少所选日期数据";
        }
        int a = thisYearGrade - lastYearGrade;
        return chuFa(a, lastYearGrade);
    }

    /**
     * 管理率
     */
    public static String getRate(Integer a, Integer b) {
        if (b == null) {
            return "分子不能为null，请核对数据";
        }
        if (a == null) {
            return "分子不能为null，请核对数据";
        }
        if(a==0 || b== 0){
            return "0.00";
        }
        return chuFa(a, b);
    }
    //求比例，返回map
    public static Map<String, String> getEchartsData(Map<String, EchartsEntity> demo1, Map<String, EchartsEntity> demo2) {
        Map<String, String> map = new HashMap<>(6);
        for (String key : demo1.keySet()) {
            if ((demo2.get(key) != null)) {
                String valueRate = getRate(Integer.valueOf((demo1.get(key).getValue())), Integer.valueOf((demo2.get(key).getValue())));
                map.put(key, valueRate);
            }
        }
        return map;
    }
    //求比例，返回集合
    public static List<EchartsEntity> getEchartsDataByList(List<EchartsEntity> demo1, List<EchartsEntity> demo2) {
        List<EchartsEntity> map = new ArrayList<EchartsEntity>();
        for (EchartsEntity entity : demo1) {
            String key = entity.getKey();
            for (EchartsEntity entity2 : demo2){
                if (entity2.getKey() != null && entity2.getKey().equals(entity.getKey())){
                    String valueRate = getRate(Integer.valueOf(entity.getValue()), Integer.valueOf(entity2.getValue()));
                    EchartsEntity echartentity = new EchartsEntity();
                    echartentity.setKey(key);
                    echartentity.setValue(valueRate);
                    map.add(echartentity);
                    break;
                }
            }
        }
        return map;
    }
    //求比例，返回集合，且按照横坐标，没有的补全
    public static List<EchartsEntity> getEchartsDataByListByYear(List<EchartsEntity> demo1, List<EchartsEntity> demo2,String[] recentYearArr ) {
        List<EchartsEntity> map = new ArrayList<EchartsEntity>();
        for(String rowkey : recentYearArr){
            EchartsEntity echartentity = new EchartsEntity();
            echartentity.setKey(rowkey);
            echartentity.setValue("0");
            for (EchartsEntity entity : demo1) {
                String key = entity.getKey();
                if (key != null && key.equals(rowkey)){
                    for (EchartsEntity entity2 : demo2){
                        if (entity2.getKey() != null && entity2.getKey().equals(entity.getKey())){
                            String valueRate = getRate(Integer.valueOf(entity.getValue()), Integer.valueOf(entity2.getValue()));
                            echartentity.setValue(valueRate);
                            break;
                        }
                    }
                    break;
                }
            }
            map.add(echartentity);
        }
        return map;
    }
    //返回集合，且按照横坐标，没有的补全
    public static List<EchartsEntity> getEchartsDataByYear(List<EchartsEntity> demo1,String[] recentYearArr ) {
        List<EchartsEntity> map = new ArrayList<EchartsEntity>();
        for(String rowkey : recentYearArr){
            if (rowkey.equals("0")){
                continue;
            }
            EchartsEntity echartentity = new EchartsEntity();
            echartentity.setKey(rowkey);
            echartentity.setValue("0");
            for (EchartsEntity entity : demo1) {
                String key = entity.getKey();
                if (key != null && key.equals(rowkey)){
                    echartentity.setValue(entity.getValue());
                    break;
                }
            }
            map.add(echartentity);
        }
        return map;
    }
    /**
     * 根据传入年份获取最近五年
     */
    public static String getRecentYear(String year) {
        int topYear = Integer.parseInt(year);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("'").append(topYear).append("','");
        stringBuilder.append(topYear - 1).append("','");
        stringBuilder.append(topYear - 2).append("','");
        stringBuilder.append(topYear - 3).append("','");
        stringBuilder.append(topYear - 4).append("'");
        return stringBuilder.toString();
    }
    /**
     * 根据传入年份获取最近五年,返回数组
     */
    public static String[] getRecentYearArr(String year) {
        String[] recentYearArr = new String[5];
        recentYearArr[0] = (Integer.parseInt(year) -4) + "";
        recentYearArr[1] = (Integer.parseInt(year) -3) + "";
        recentYearArr[2] = (Integer.parseInt(year) -2) + "";
        recentYearArr[3] = (Integer.parseInt(year) -1) + "";
        recentYearArr[4] = year;
        return recentYearArr;
    }
    /**
     * 计算平均数
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/15 15:01
     */
    public static String getAverage(List<EchartsEntity> hxdjb) {
        float sum = 0;
        int count = hxdjb.size();
        for (EchartsEntity entity : hxdjb){
            sum += Float.parseFloat(entity.getValue());
        }
        return Math.round(sum/count * 100) / 100.0 + "";
    }
}
